<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="teacher/teacher-common::commonCss(~{::title},~{},~{::script},~{::style})">
    <title>JAVA在线考试-成绩管理</title>

    <script th:src="@{/laydate/laydate.js}"></script>
    <style>
        .body{
            background-color: #f8f8f8;
        }
    </style>

    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <button lay-event="deletes" class="layui-btn layui-btn-danger">删除选中行</button>
            <button lay-event="export" class="layui-btn layui-btn-warm">导出成绩</button>
        </div>
    </script>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a><br/>
    </script>
    <script th:inline="javascript">
        layui.use(['table','element','laydate','form'], function(){
            var table = layui.table;
            var element = layui.element;
            var laydate = layui.laydate;
            var form = layui.form;

            laydate.render({
                elem: '#search-createTime'
                ,range: true
            });

            $("#searchBtn").click(function () {
                reload();
            });
            $("#resetBtn").click(function () {
                $("#searchgradeInfoForm")[0].reset();
                tableIns.reload({
                    where: {
                        "msg": "",
                        "code": "",
                        "data": {
                            "username": '',
                            "title": '',
                            "startTime": '',
                            "endTime": ''
                        }
                    }
                    , page: {
                        curr: 1
                    }
                });
            });

            var tableIns = table.render({
                elem: '#gradeTable'
                , url: '/grade/listForPage'
                , toolbar: '#toolbar'
                , contentType: 'application/json'
                , method: 'post'
                , where: {
                    "msg": "",
                    "code": "",
                    "data": {
                        "username": $('#search-username').val(),
                        "title": $('#search-title').val(),
                        "startTime": $("#search-createTime").val().split(" - ")[0],
                        "endTime": $("#search-createTime").val().split(" - ")[1] != null ? $("#search-createTime").val().split(" - ")[1] : ""
                    }
                }
                , defaultToolbar: []
                , page: true
                , limits: [5, 10, 20]
                , limit: 10
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left'}
                        , {field: 'id', title: 'ID', hide: true}
                        , {field: 'userId', title: 'userId', hide: true}
                        , {field: 'examId', title: 'examId', hide: true}
                        , {field: 'username', title: '用户名'}
                        , {field: 'name', title: '姓名'}
                        , {field: 'title', title: '考试名'}
                        , {field: 'attendTime', title: '开始时间'}
                        , {field: 'finishTime', title: '完成试卷'}
                        , {field: 'userScore', title: '成绩'}
                        , {field: 'finishFlag', title: '完成状态', templet: function (d) {
                            var type = d.finishFlag;
                            if(type===0){
                                return '<span style="color:green">答题中</span>';
                            }else if(type===1){
                                return '<span>待批卷</span>';
                            }else if(type===2){
                                return '<span>已完成</span>';
                            }else if(type===-1){
                                return '<span style="color:red">未参加</span>';
                            }
                        }}
                        , {field: 'createTime', title: '创建时间'}
                        , {title: '操作', fixed: 'right', width: 135, toolbar: '#bar'}
                    ]
                ]
                ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.result.code, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.records //解析数据列表
                    };
                }
            });

            //监听工具条
            table.on('tool(gradeTable)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case 'delete':
                        layer.confirm('真的删除么', function (index) {
                            $.ajax({
                                url : '/grade/delete/'+data.id,
                                type : 'post',
                                dataType : 'json',
                                success : function(n) {
                                    if (n.result.status) {
                                        reload();
                                    }else {
                                        layer.msg("删除成绩失败,"+n.result.msg);
                                    }
                                },
                                error: function(n) {
                                    layer.msg("删除成绩失败,"+n.responseJSON.result.msg);
                                }
                            });
                            layer.close(index);
                        });
                        break;
                }
                ;
            });
            table.on('toolbar(gradeTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'deletes':
                        var data = checkStatus.data;
                        var size = data.length;
                        if (size != 0) {
                            layer.confirm('真的删除么', function (index) {
                                for (let i = 0; i < size; i++) {
                                    $.ajax({
                                        url : '/grade/delete/'+data[i].id,
                                        type : 'post',
                                        dataType : 'json',
                                        success : function(n) {
                                            if (n.result.status) {
                                                reload();
                                            }else {
                                                layer.msg("删除成绩失败,"+n.result.msg);
                                            }
                                        },
                                        error: function(n) {
                                            layer.msg("删除成绩失败,"+n.responseJSON.result.msg);
                                        }
                                    });
                                }
                                layer.close(index);
                                reload();
                            });
                        } else {
                            layer.msg("请勾选要删除的成绩");
                        }
                        break;
                    case 'export':
                        var username = $('#search-username').val();
                        var title = $('#search-title').val();
                        var startTime = $("#search-createTime").val().split(" - ")[0];
                        var endTime = $("#search-createTime").val().split(" - ")[1] != null ? $("#search-createTime").val().split(" - ")[1] : "";
                        var url = '/grade/export?' +
                                'username='+ username +'&'+
                                'title='+ title +'&'+
                                'startTime='+ startTime +'&'+
                                'endTime='+ endTime;
                        console.log(url);
                        window.open(url);
                        break;
                } ;
            });

            function reload() {
                tableIns.reload({
                    where: {
                        "msg": "",
                        "code": "",
                        "data": {
                            "username": $('#search-username').val(),
                            "title": $('#search-title').val(),
                            "startTime": $("#search-createTime").val().split(" - ")[0],
                            "endTime": $("#search-createTime").val().split(" - ")[1] != null ? $("#search-createTime").val().split(" - ")[1] : ""
                        }
                    }
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }

        });

    </script>
</head>
<body>
<div class="layui-layout layui-layout-admin">

    <div th:replace="teacher/teacher-common::header"></div>
    <div th:replace="teacher/teacher-common::side('grade')"></div>

    <div class="layui-body body">
        <div class="layui-fluid" style="padding: 15px;">
            <div class="layui-card">
                <form class="layui-form layui-card-header" style="padding: 15px" lay-filter="searchgradeInfoForm" id="searchgradeInfoForm">
                    <div class="layui-form-item">
                        <div class="layui-inline" style="width:150px;">
                            <input class="layui-input" id="search-username" autocomplete="off" placeholder="用户名">
                        </div>
                        <div class="layui-inline" style="width:150px;">
                            <input class="layui-input" id="search-title" autocomplete="off" placeholder="考试标题">
                        </div>
                        <div class="layui-inline">
                            <input type="text" id="search-createTime" placeholder="创建时间" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline" style="float: right;">
                            <button type="button" class="layui-btn" id="searchBtn">
                                <i class="layui-icon layui-icon-search"></i> 搜索
                            </button>
                            <button  type="button" class="layui-btn" id="resetBtn">重置</button>
                        </div>

                    </div>
                </form>

                <div class="layui-card-body">
                    <div style="padding-bottom:10px;">
                        <table class="layui-hide" id="gradeTable" lay-skin="line" lay-filter="gradeTable">
                        </table>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <div th:replace="teacher/teacher-common::footer"></div>
</div>
</body>
</html>